package util;

import io.javalin.http.UploadedFile;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class ValidationUtil {
    // 文件类型限制
    private static final Set<String> ALLOWED_IMAGE_TYPES = new HashSet<>(Arrays.asList(
        "image/jpeg", "image/png", "image/gif"
    ));
    
    private static final Set<String> ALLOWED_AUDIO_TYPES = new HashSet<>(Arrays.asList(
        "audio/mpeg", "audio/mp3", "audio/wav"
    ));
    
    private static final Set<String> ALLOWED_LYRIC_TYPES = new HashSet<>(Arrays.asList(
        "text/plain", "application/x-subrip"
    ));
    
    // 文件大小限制（单位：字节）
    private static final long MAX_IMAGE_SIZE = 5 * 1024 * 1024;  // 5MB
    private static final long MAX_AUDIO_SIZE = 20 * 1024 * 1024; // 20MB
    private static final long MAX_LYRIC_SIZE = 100 * 1024;       // 100KB

    // 用户相关验证
    public static void validateUsername(String username) {
        if (username == null || username.trim().isEmpty()) {
            throw new IllegalArgumentException("用户名不能为空");
        }
        if (username.length() > 50) {
            throw new IllegalArgumentException("用户名不能超过50个字符");
        }
    }

    public static void validatePassword(String password) {
        if (password == null || password.trim().isEmpty()) {
            throw new IllegalArgumentException("密码不能为空");
        }
        if (password.length() < 6) {
            throw new IllegalArgumentException("密码长度不能少于6个字符");
        }
        if (password.length() > 50) {
            throw new IllegalArgumentException("密码长度不能超过50个字符");
        }
    }

    // 歌单相关验证
    public static void validatePlaylistName(String name) {
        if (name == null || name.trim().isEmpty()) {
            throw new IllegalArgumentException("歌单名称不能为空");
        }
        if (name.length() > 50) {
            throw new IllegalArgumentException("歌单名称不能超过50个字符");
        }
    }

    public static void validatePlaylistDescription(String description) {
        if (description != null && description.length() > 200) {
            throw new IllegalArgumentException("歌单描述不能超过200个字符");
        }
    }

    // 音乐相关验证
    public static void validateMusicTitle(String title) {
        if (title == null || title.trim().isEmpty()) {
            throw new IllegalArgumentException("音乐标题不能为空");
        }
        if (title.length() > 100) {
            throw new IllegalArgumentException("音乐标题不能超过100个字符");
        }
    }

    public static void validateArtist(String artist) {
        if (artist == null || artist.trim().isEmpty()) {
            throw new IllegalArgumentException("艺术家名称不能为空");
        }
        if (artist.length() > 100) {
            throw new IllegalArgumentException("艺术家名称不能超过100个字符");
        }
    }

    // 颜色代码验证
    public static void validateColorCode(String colorCode) {
        if (colorCode != null && !colorCode.matches("^#[0-9A-Fa-f]{6}$")) {
            throw new IllegalArgumentException("无效的颜色格式");
        }
    }

    // 文件验证方法
    public static void validateImageFile(UploadedFile file) {
        if (file == null) {
            throw new IllegalArgumentException("请选择要上传的图片");
        }
        if (!ALLOWED_IMAGE_TYPES.contains(file.getContentType())) {
            throw new IllegalArgumentException("只支持 JPG/PNG/GIF 格式的图片");
        }
        if (file.getSize() > MAX_IMAGE_SIZE) {
            throw new IllegalArgumentException("图片大小不能超过 5MB");
        }
    }

    public static void validateAudioFile(UploadedFile file) {
        if (file == null) {
            throw new IllegalArgumentException("请选择要上传的音频文件");
        }
        if (!ALLOWED_AUDIO_TYPES.contains(file.getContentType())) {
            throw new IllegalArgumentException("只支持 MP3/WAV 格式的音频");
        }
        if (file.getSize() > MAX_AUDIO_SIZE) {
            throw new IllegalArgumentException("音频文件大小不能超过 20MB");
        }
    }

    public static void validateLyricFile(UploadedFile file) {
        String filename = file.getFilename().toLowerCase();
        if (!filename.endsWith(".lrc")) {
            throw new IllegalArgumentException("只支持 .lrc 格式的歌词文件");
        }
    }
}
